﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using StrategyGame.Model;

namespace StrategyGameSilverlight.UserControls
{
    public partial class FamilyTreeControl : UserControl
    {
        public int Generations { get; set; }

        private Person _person;
        public Person Person
        {
            get { return _person; }
            set
            {
                _person = value;
                OnPersonChanged();
            }
        }

        public FamilyTreeControl()
        {
            InitializeComponent();
            
            LayoutRoot.ShowGridLines = true;
        }

        private int _generationsLeft = 0; 
        private void OnPersonChanged()
        {
            int peopleCount = 1;
            LayoutRoot.ColumnDefinitions.Clear();
            LayoutRoot.RowDefinitions.Clear();
            LayoutRoot.Children.Clear();

            LayoutRoot.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto, MinWidth = 10 });
            LayoutRoot.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto, MinHeight = 10 });
            for (int x = 0; x < Generations; x++)
            {
                LayoutRoot.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto, MinWidth = 10 });
                LayoutRoot.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto, MinWidth = 10 });
                peopleCount += (x + 1) * 2;
            }
            for (int y = 0; y < peopleCount; y++)
            {
                LayoutRoot.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto, MinHeight = 10 });
            }
            
            _generationsLeft = Generations;
            AddPerson(_person, 0, (Generations*2)-1);
        }

        private void AddPerson(Person person, int x, int y)
        {
            FamilyTreeControl tree = this;

            PersonControl personControl = new PersonControl { DataContext = person };
            personControl.button1.Click += (s, e) => { Person = (Person)personControl.DataContext; };

            LayoutRoot.Children.Add(personControl);
            Grid.SetColumn(personControl, x);
            
            Grid.SetRow(personControl, y);
            Grid.SetRowSpan(personControl, 2);

            DrawParents(person, x, y);
        }

        private void DrawParents(Person person, int x, int y)
        {
            if (_generationsLeft <= 0) return;
            _generationsLeft--;
            if (person.Father != null) AddPerson(person.Father, x + 2, (y * (Generations -1)) - _generationsLeft -1);
            if (person.Mother != null) AddPerson(person.Mother, x + 2, (y * (Generations - 1)) + _generationsLeft + 1);
            _generationsLeft++;
        }
    }
}
